/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

package cheat.entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * Represents personal information for either an author or a user of the system.
 *
 * @author Keith Shie
 */
@Entity
@Table(name = "person")
@NamedQueries({@NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),
        @NamedQuery(name = "Person.findByPersonId", query = "SELECT p FROM Person p WHERE p.personId = :personId"),
        @NamedQuery(name = "Person.findByFirstName", query = "SELECT p FROM Person p WHERE p.firstName = :firstName"),
        @NamedQuery(name = "Person.findByLastName", query = "SELECT p FROM Person p WHERE p.lastName = :lastName"),
        @NamedQuery(name = "Person.findByEmail", query = "SELECT p FROM Person p WHERE p.email = :email"),
        @NamedQuery(name = "Person.findByPhone", query = "SELECT p FROM Person p WHERE p.phone = :phone"),
        @NamedQuery(name = "Person.findByAffiliation", query = "SELECT p FROM Person p WHERE p.affiliation = :affiliation"),
        @NamedQuery(name = "Person.findByUrl", query = "SELECT p FROM Person p WHERE p.url = :url")})
public class Person implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @SequenceGenerator(name="person_seq", sequenceName="person_person_id_seq", allocationSize=1)
  @GeneratedValue( strategy=GenerationType.SEQUENCE,generator="person_seq")
  @Column(name = "person_id", nullable = false)
  private Integer personId;
  @Basic(optional = false)
  @Column(name = "first_name", nullable = false, length = 30)
  private String firstName;
  @Basic(optional = false)
  @Column(name = "last_name", nullable = false, length = 30)
  private String lastName;
  @Basic(optional = false)
  @Column(name = "email", nullable = false, length = 80)
  private String email;
  @Column(name = "phone", length = 14, unique=true)
  private String phone;
  @Column(name = "affiliation", length = 80)
  private String affiliation;
  @Column(name = "url", length = 256)
  private String url;
  @ManyToMany(mappedBy = "personCollection")
  private Collection<AssignmentProperty> asgnPropCollection;
  @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
  private RegisteredUser regUser;

  public Person() {
  }

  public Person(Integer personId) {
    this.personId = personId;
  }

  public Person(Integer personId, String firstName, String lastName, String email) {
    this.personId = personId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
  }

  public Integer getPersonId() {
    return personId;
  }

  public void setPersonId(Integer personId) {
    this.personId = personId;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getAffiliation() {
    return affiliation;
  }

  public void setAffiliation(String affiliation) {
    this.affiliation = affiliation;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public Collection<AssignmentProperty> getAsgnPropCollection() {
    return asgnPropCollection;
  }

  public void setAsgnPropCollection(Collection<AssignmentProperty> asgnPropCollection) {
    this.asgnPropCollection = asgnPropCollection;
  }

  public RegisteredUser getRegisteredUser() {
    return regUser;
  }

  public void setRegisteredUser(RegisteredUser regUser) {
    this.regUser = regUser;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (personId != null ? personId.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Person)) {
      return false;
    }
    Person other = (Person) object;
    if ((this.personId == null && other.personId != null) || (this.personId != null && !this.personId.equals(other.personId))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "cheat.entities.Person[personId=" + personId + "]";
  }

}
